/**
	加载页面
*/
let load = {
	el1 : null, //放新生成的oLogo
	el2 : null, //放新生成的oLoading
	//生成loading界面
	create : function(){
		var 
			iNum     = 1,
			iTop     = 10,
			iTimer   = null,
			oBox     = $('box'),
			oLogo    = createHtml('div'),
			oLoading = createHtml('img');
		//背景图移动
		setInterval(function(){
			iTop += 4;
			oBox.style.backgroundPosition = '0 '+ iTop + 'px';
		},50);
		//存储
		this.el1 = oLogo; 
		this.el2 = oLoading; 
		//装饰
		oLogo.className = 'logo';
		oLoading.className = 'loading';
		oLoading.src = 'img/loading'+ iNum +'.png';
		iTimer = setInterval(function(){
			iNum++;
			oLoading.src = 'img/loading'+ iNum +'.png';
			if (iNum === 3) {
				iNum = 0;
			}
		},300)
		//装入Dom节点中
		oBox.appendChild(oLogo);
		oBox.appendChild(oLoading);
	},

	//关闭loading页面
	destroy: function(){
		var 
			iTimer = null;
			oBox   = $('box'),
			oScore = $('score');
		iTimer = setTimeout(function(){
			oBox.removeChild(this.el1); //清除logo
			oBox.removeChild(this.el2);	//清除loading
			clearTimeout(iTimer); //清除定时器

			oScore.style.display = 'block'; //生成分数板
			me.create();    //生成己方飞机
			me.createenemy(); // 生成敌方飞机
		}.bind(this),1000)
	},

};
/**
	生成飞机
*/
let me = {
	el : null,
	iTimer:null,
	ifrequency:null, //发射频率
	//生成
	create : function(){
		var 
			iWinW = document.documentElement.clientWidth,
			iWinH = document.documentElement.clientHeight,
			oMe   = createHtml('div');
		oMe.className  = 'me';
		document.body.appendChild(oMe);
		this.el = oMe;
		this.el.style.top  = iWinH - this.el.offsetHeight + 'px';
		this.el.style.left = (iWinW - this.el.offsetWidth) / 2 + 'px'; 
		this.move().bullet();
	},
	//运动
	move : function(){
		document.onmousemove = function(e){
			var 
				ev   = e || window.event,
				oBox =	$('box'),
				iL   = ev.clientX - this.el.offsetWidth / 2;
			//限定运动范围
			if (iL <= oBox.offsetLeft) {
				iL = oBox.offsetLeft;
			};
			if (iL >= oBox.offsetLeft + oBox.offsetWidth - this.el.offsetWidth) {
				iL = oBox.offsetLeft + oBox.offsetWidth - this.el.offsetWidth;
			};

			this.el.style.left = iL + 'px';
		}.bind(this);
		return this;
	},
	//发射子弹
	bullet : function(){
		iTimer = setInterval(function(){
			var oBiu = new biu();
			oBiu.create().up();
		},this.ifrequency)
	},
	//生成敌方飞机
	createenemy : function(){
		//创造小飞机
		setInterval(function(){
			Math.random() > 0.4 ? new enemies().create(enemies.prototype.small).move() : '';
		},500)
		//创造中型飞机
		setInterval(function(){
			Math.random() > 0.5 ? new enemies().create(enemies.prototype.middle).move() : '';
		},1200)
		//创造打大飞机
		setInterval(function(){
			Math.random() > 0.7 ? new enemies().create(enemies.prototype.big).move() : '';
		},2000)
	}
}


/**
	控制游戏运行的引擎
*/
let play = {
	oEnemies : {}, //放敌机
	oBius : {}, //放子弹
	aScore : [], //记录分数
	el : null,
	iNum : 0,
	score:0,
	starNum : null,
	//加载游戏
	loading : function(){
		if (!load.el1) {
			load.create();
		}
	},
	//游戏运行
	work : function(){
		load.destroy();
	},
	//重新开始
	rework : function(){
		if (!load.el1) {
			me.create();    //生成己方飞机
			me.createenemy(); // 生成敌方飞机
		}
	},
	//击杀
	kill : function(){
		setInterval(function(){
			for(var sBius in play.oBius){
				for(var sEnemies in play.oEnemies){
					if (pz(play.oBius[sBius].el,play.oEnemies[sEnemies].el)) {
						play.oBius[sBius].destroy(); //子弹移除

						if(play.iNum >= 5){
							play.oEnemies[sEnemies].iBlood -= 10; //敌机掉血-10
							play.score ++;
						}else{
							play.oEnemies[sEnemies].iBlood --; //敌机掉血-1
							play.score ++;
						}

						//判断是否触发消灭事件
						if (play.oEnemies[sEnemies].iBlood <= 0) {
							play.oEnemies[sEnemies].destroy(); //移除敌机
							play.iNum++; //计算打中的敌机数量
						}
						break;
					}
				}
			}
		},30)
	},
	//奖励特效
	reward : function(){
		var oBox = $('box');
		oBox.style.backgroundImage = "url(img/bg3.jpg)";
			var oBoom = new boom();
			oBoom.create().move();
	},
	//排名
	sort : function(){
		//显示排名
		var oSort = $('sort');
		oSort.innerHTML = '';
		play.aScore.forEach(function(e,v){
			var oLi = createHtml('li');

			if (e === findmax(play.aScore)) {
				oLi.className = 'num-one';

			};

			oLi.innerHTML = '第'+ (v + 1) +'次成绩' + e;
			oSort.appendChild(oLi);
		});
	},
	//游戏结束\死亡
	gameover : function(){
		var iWinW = document.documentElement.clientWidth;
		if (iWinW >= 700) {
			play.sort();
		}
		var oGover = $('gameover');
		setInterval(function(){
			for(var sEnemies in play.oEnemies){
				if (pz(me.el,play.oEnemies[sEnemies].el)) {
					play.oEnemies[sEnemies].destroy(); //移除敌机

					me.el.style.display = 'none';
					oGover.style.display = 'block';
					break;
				}
			}
		},30)
	},
	//退出游戏
	exit : function(){
		window.close();
		document.onmousemove = null;
	}
}